﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;

namespace RoslynProcess
{
    class LinesReader : IText
    {
        private readonly System.IO.TextReader textReader;
        private readonly int lineCount;
        private readonly string text;

        public LinesReader(System.IO.TextReader textReader, int lineCount)
        {
            this.textReader = textReader;
            this.lineCount = lineCount;
            this.text = ReadLinesAsString(textReader, lineCount);
        }

        public static string ReadLinesAsString(System.IO.TextReader textReader, int lineCount)
        {
            string str = "";
            // TODO Better way to do this?
            for (int i = 0; i < lineCount; i++)
            {
                string line = textReader.ReadLine();
                str += line + "\n";
            }
            return str;
        }
        public static IText ReadLinesAsIText(System.IO.TextReader textReader, int lineCount)
        {
            return new StringText(ReadLinesAsString(textReader, lineCount));
        }

        public ITextContainer Container
        {
            get { throw new NotImplementedException(); }
        }

        public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            text.CopyTo(sourceIndex, destination, destinationIndex, count);
        }

        public ITextLine GetLineFromLineNumber(int lineNumber)
        {
            throw new NotImplementedException();
        }

        public ITextLine GetLineFromPosition(int position)
        {
            throw new NotImplementedException();
        }

        public int GetLineNumberFromPosition(int position)
        {
            throw new NotImplementedException();
        }

        public string GetText(TextSpan span)
        {
            return text.Substring(span.Start, span.Length);
        }

        public string GetText()
        {
            return text;
        }

        public int Length
        {
            get { return text.Length; }
        }

        public int LineCount
        {
            get { return lineCount; }
        }

        public IEnumerable<ITextLine> Lines
        {
            get { throw new NotImplementedException(); }
        }

        public void Write(System.IO.TextWriter textWriter)
        {
            textWriter.Write(text);
        }

        public char this[int position]
        {
            get { return text[position]; }
        }
    }
}
